import { Toast,Dialog  } from 'vant';

function formatNumber (n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}


export function formatTime (date,deadline=0) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()+ +deadline

  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  const t1 = [year, month, day].map(formatNumber).join('/')
  const t2 = [hour, minute, second].map(formatNumber).join(':')

  return `${t1} ${t2}`
}
export function reverseData(data){
  let pattern=/^\d{4}-\d{2}-(\d{2})/
  return data.match(pattern)[1]
  
}

export function previewImage(current,urls){
  wx.previewImage({
    current: current, // 当前显示图片的http链接
    urls: urls, // 需要预览的图片http链接列表
    fail:err=>{console.log(err)}
  })
}

export function navigateTo(url){
  console.log(url);
  this.$router.push({path:url})
}


export function redirectTo(url){
  console.log(url)
  wx.redirectTo({
    url:url,
    fail:err=>{
      console.log(err)
    }
  })
}

export function switchTab(url){
  console.log(url)
  wx.switchTab({
    url:url,
    fail:err=>{
      console.log(err)
    }
  })
}

export function sexTag(value){
  if(value=="男"){
    return 1
  }else if(value=="女"){
    return 2
  }else{
    return 0
  }
}

export function sexTag1(value){
  if(value==1){
    return '男'
  }else if(value==2){
    return '女'
  }else{
    return '男女不限'
  }
}

export function showToast(message='修改成功45646',time=1500,show='success'){
  if(show=='success'){
    Toast.success({
      message: message,
      duration:time
    })
  }else{
    Toast.fail({
      message: message,
      duration:time
    })
  }
}

export function share(res,title,path){
  return {
    title:title,
    path:path,
    success(res){
      wx.showToast({
        title:"转发成功",
      })
    },
    fail(err){
      console.log(err)
    }
  }
}

//写cookies 
export function setCookie(name,value){ 
  var Days = 30; 
  var exp = new Date(); 
  exp.setTime(exp.getTime() + Days*24*60*60*1000); 
  document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); 
} 

//读取cookies 
export function getCookie(name){ 
  var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
  if(arr=document.cookie.match(reg))
    return unescape(arr[2]); 
  else 
    return null; 
} 
//删除cookies 
export function delCookie(name) { 
    var exp = new Date(); 
    exp.setTime(exp.getTime() - 1); 
    var cval=getCookie(name); 
    if(cval!=null){
      document.cookie= name + "="+cval+";expires="+exp.toGMTString();
    }
}

//获取URL参数
function getQueryVariable(variable){
  var url = window.location.href.split("?");
  if(url[1] == '' || url[1] == undefined){
    return (false)
  }
  var vars = url[1].split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if(pair[0] == variable){return pair[1];}
  }
  return (false)
}


//格式化时间 Thu Mar 07 2019 12:00:00 GMT+0800 (中国标准时间) 转换为 2019-03-07 12:00:00
function transDate(date){
  const d = new Date(date);
  const resDate = d.getFullYear() + '-' + p((d.getMonth() + 1)) + '-' + p(d.getDate());
  const resTime = p(d.getHours()) + ':' + p(d.getMinutes())
  // const resTime = p(d.getHours()) + ':' + p(d.getMinutes()) + ':' + p(d.getSeconds());
  function p(s) {
    return s < 10 ? '0' + s : s
  }
  return resDate+' '+resTime
}

export default {
  formatNumber,
  formatTime,
  navigateTo,
  showToast,
  redirectTo,
  previewImage,
  switchTab,
  sexTag,
  sexTag1,
  share,
  setCookie,
  getCookie,
  delCookie,
  getQueryVariable,
  transDate
}
